package com.lmc.form.dao.dal;


import com.free.bsf.core.db.DbConn;
import com.free.bsf.core.util.BeanCopyUtil;
import com.free.bsf.core.util.ConvertUtils;
import com.lmc.form.dao.dal.auto.paas_form_data_base_dal;
import com.lmc.form.dao.dal.auto.paas_form_model_base_dal;
import com.lmc.form.dao.model.auto.paas_form_data_model;
import com.lmc.form.dao.model.auto.paas_form_model_history_model;
import com.lmc.form.dao.model.auto.paas_form_model_model;
import lombok.val;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * paas_form_data 表自动dal映射,不要手工修改
 * 
 * @author 车江毅
 * @since 2022-03-21 14:01:54
 * 自动生成: https://gitee.com/makejava/EasyCode/wikis/
 */
public class paas_form_model_dal extends paas_form_model_base_dal {

    public paas_form_model_model get(DbConn conn, String key) {
        val par = new Object[]{key};
        val stringSql = new StringBuilder();
        stringSql.append("select s.* from paas_form_model s where s.form_key=?");
        val ds = conn.executeList(stringSql.toString(), par);
        if (ds != null && ds.size() > 0) {
            return createModel(ds.get(0));
        }
        return null;
    }

    public List<paas_form_model_model> getSubForms(DbConn conn) {
        val rs = new ArrayList<paas_form_model_model>();
        val par = new Object[]{};
        val stringSql = new StringBuilder();
        stringSql.append("select id,form_key,form_name,create_time,update_time from paas_form_model s where s.form_type=2");
        val ds = conn.executeList(stringSql.toString(), new Object[]{});
        if (ds != null && ds.size() > 0) {
            for (Map<String, Object> dr : ds) {
                rs.add(createModel(dr));
            }
        }
        return rs;
    }

    public boolean addOrUpdate(DbConn conn, paas_form_model_model model) {
        val o = get(conn,model.form_key);
        if(o==null){
            model.version=1;
            add(conn,model);
        }else{
            model.version=o.version+1;
            val history = BeanCopyUtil.copyProperties(o, paas_form_model_history_model::new);
            new paas_form_model_history_dal().add(conn,history);
            delete(conn,o.id);
            add(conn,model);
        }
        return true;
    }

    public boolean deleteByKey(DbConn conn, String form_key) {
        val par = new Object[]{form_key};
        String Sql = "delete from paas_form_model where form_key=?";
        int rev = conn.executeSql(Sql, par);
        if (rev == 1) {
            return true;
        } else {
            return false;
        }
    }
}


